home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / CBASE.ZIP / CBCREATE.C < prev    next >
Text File  |  1990-06-21  |  4KB  |  156 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbcreate.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stddef.h>*/
  9. #include <stdio.h>
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13. #include <btree.h>
  14. #include <lseq.h>
  15.  
  16. /* local headers */
  17. #include "cbase_.h"
  18.  
  19. /* function declarations */
  20. #ifdef AC_PROTO
  21. int cbrposcmp(const void *p1, const void *p2, size_t n);
  22. #else
  23. int cbrposcmp();
  24. #endif
  25.  
  26. /* btree field definition list */
  27. static btfield_t btfldv[] = {
  28.     {0,
  29.      0,
  30.      NULL,
  31.      BT_FASC
  32.     },
  33.     {0,
  34.      sizeof(cbrpos_t),
  35.      cbrposcmp,
  36.      BT_FASC
  37.     }
  38. };
  39.  
  40. /*man---------------------------------------------------------------------------
  41. NAME
  42.      cbcreate - create a cbase
  43.  
  44. SYNOPSIS
  45.      #include <cbase.h>
  46.  
  47.      int cbcreate(cbname, recsize, fldc, fldv)
  48.      const char *cbname;
  49.      size_t recsize;
  50.      int fldc;
  51.      const cbfield_t fldv[];
  52.  
  53. DESCRIPTION
  54.      The cbcreate function creates a cbase.  cbname points to a
  55.      character string that contains the name of the cbase to be
  56.      created.  cbname is used as the name of the data file containing
  57.      the records in the cbase.
  58.  
  59.      recsize specifies the size of the records in the cbase.
  60.  
  61.      fldc is the field count.  It specifies the number of fields in
  62.      the records stored in this cbase.  fldv is an array of field
  63.      definition structures.  fldv must have fldc elements.  The field
  64.      definition structure is defined in <cbase.h> as type cbfield_t.
  65.      It has the following members.
  66.  
  67.           size_t offset;      /* offset of field in record *\/
  68.           size_t len;         /* field length *\/
  69.           int type;           /* field data type *\/
  70.           int flags;          /* flags *\/
  71.           char filename[FILENAME_MAX + 1];
  72.                               /* name of key file *\/
  73.  
  74.      offset and len specify the location and length of the field,
  75.      respectively.  type is the data type for the field; see cbase
  76.      manual entry for a list of the predefined data types, and
  77.      the cbase Programmer's Guide for information on adding new data
  78.      types.  filename is the name of the file to be used for key
  79.      storage.  flags values are constructed by OR-ing flags from the
  80.      following list:
  81.  
  82.      CB_FKEY        Field is a key.
  83.      CB_FUNIQ       Only for use with CB_FKEY.  Indicates
  84.                     that the keys must be unique.
  85.  
  86.      The fields in the field definition list must be in order,
  87.      starting with the first field in the record.
  88.  
  89.      cbcreate will fail if one or more of the following is true:
  90.  
  91.      [EEXIST]       Either the record file or one of
  92.                     the key files exists.
  93.      [EINVAL]       cbname is the NULL pointer.
  94.      [EINVAL]       recsize is less than sizeof(cbrpos_t).
  95.      [EINVAL]       fldc is less than 1.
  96.      [EINVAL]       fldv is the NULL pointer.
  97.      [EINVAL]       fldv  contains an invalid field
  98.                     definition.
  99.  
  100. SEE ALSO
  101.      cbopen.
  102.  
  103. DIAGNOSTICS
  104.      Upon successful completion, a value of 0 is returned.  Otherwise,
  105.      a value of -1 is returned, and errno set to indicate the error.
  106.  
  107. ------------------------------------------------------------------------------*/
  108. int cbcreate(cbname, recsize, fldc, fldv)
  109. const char *cbname;
  110. size_t recsize;
  111. int fldc;
  112. const cbfield_t fldv[];
  113. {
  114.     int terrno = 0;
  115.     int i = 0;
  116.  
  117.     /* validate arguments */
  118.     if (cbname == NULL || recsize < sizeof(cbrpos_t)) {
  119.         errno = EINVAL;
  120.         return -1;
  121.     }
  122.     if (!cb_fvalid(recsize, fldc, fldv)) {
  123.         errno = EINVAL;
  124.         return -1;
  125.     }
  126.  
  127.     /* create data file */
  128.     if (lscreate(cbname, recsize) == -1) {
  129.         if (errno != EEXIST) CBEPRINT;
  130.         return -1;
  131.     }
  132.  
  133.     /* create key files */
  134.     for (i = 0; i < fldc; ++i) {
  135.         if (fldv[i].flags & CB_FKEY) {
  136.             btfldv[1].offset = btfldv[0].len = fldv[i].len;
  137.             btfldv[0].cmp = cbcmpv[fldv[i].type];
  138.             if (btcreate(fldv[i].filename, CBM, fldv[i].len + sizeof(cbrpos_t), 2, btfldv) == -1) {
  139.                 if (errno != EEXIST) CBEPRINT;
  140.                 terrno = errno;
  141.                 for (i--; i >= 0; i--) {    /* remove files */
  142.                     if (fldv[i].flags & CB_FKEY) {
  143.                         remove(fldv[i].filename);
  144.                     }
  145.                 }
  146.                 remove(cbname);
  147.                 errno = terrno;
  148.                 return -1;
  149.             }
  150.         }
  151.     }
  152.  
  153.     errno = 0;
  154.     return 0;
  155. }
  156.